package com.ftqh.project.travel.security.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ftqh.project.travel.notes.entity.Note;
import com.ftqh.project.travel.notes.service.NoteService;
import com.ftqh.project.travel.security.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Administrator on 2017/4/21.
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    NoteService noteService;

    @RequestMapping(value = "isExistUsername",produces = "application/json")
    public JSONObject isExistUsername(String username) {
        StringBuffer jsonString = new StringBuffer("{'result':");
        Boolean bool = userService.findByUsername(username).isPresent();
        jsonString.append(bool);
        jsonString.append("}");
        return JSON.parseObject(jsonString.toString());
    }
    @RequestMapping(value = "isExistPhone",produces = "application/json")
    public JSONObject isExistPhone(String phone) {
        StringBuffer jsonString = new StringBuffer("{'result':");
        Boolean bool = userService.findByPhone(phone).isPresent();
        jsonString.append(bool);
        jsonString.append("}");
        return JSON.parseObject(jsonString.toString());
    }
    @GetMapping("/{authorName}/notes")
    public List<Note> findByAuthorName(@PathVariable("authorName") String authorName){
        return noteService.findByAuthorName(authorName);
    }
}
